package bbkj.org.cn.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import lombok.extern.slf4j.Slf4j;
 
@RestController
@RequestMapping("/nacos-config")
@RefreshScope
@Slf4j
public class NacosConfigController {
	@Autowired
	private ApplicationContext applicationContext;
	//1 硬编码方式
	@GetMapping("/test1")
	@SentinelResource(value = "test1",blockHandler = "handleException")
	public String nacosConfingTest1() {
//		try {
//			TimeUnit.SECONDS.sleep(3);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
		try {
			log.info("->"+InetAddress.getLocalHost());
		} catch (UnknownHostException e) {
			log.error("UnknownHostException->",e);
		}
		return applicationContext.getEnvironment().getProperty("config.appName");
	}
	
	public String handleException(BlockException ex) {
        return "Error: " + ex.getClass().getSimpleName();
    }
 
	@Value("${config.appName:}")
	private String appName;
	//2 注解方式
	@GetMapping("/test2")
	public String nacosConfingTest2() {
		return appName;
	}
 
 
 
}